import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:rabbit_mobile/view/Oder_payment/widget/payments_address.dart';
import 'package:rabbit_mobile/view/Oder_payment/widget/payments_footer.dart';
import 'package:rabbit_mobile/view/Oder_payment/widget/payments_goods.dart';
import 'package:rabbit_mobile/view/Oder_payment/widget/payments_information.dart';
import 'package:rabbit_mobile/view/Oder_payment/widget/payments_price.dart';
import 'package:rabbit_mobile/widget/appbar_widget/index.dart';
import 'package:rabbit_mobile/widget/price_Widget/index.dart';

import '../../provide/riverpod_provider.dart';
import 'models/payments_model.dart';

class OrderPayment extends ConsumerStatefulWidget {
  const OrderPayment({super.key});

  @override
  _OrderPaymentState createState() => _OrderPaymentState();
}

class _OrderPaymentState extends ConsumerState<OrderPayment> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final AsyncValue<PaymentsModel> paymentData = ref.watch(paymentsGoodsProvider);
    return Scaffold(
      appBar: const AppBarWidget(
        imgPath: 'assets/home_appBar_bg.png',
        title: "填寫訂單",
        turnIcon: true,
      ),
      body: paymentData.when(
        data: (data) {
          return Stack(
            children: [
              Padding(
                padding: const EdgeInsets.only(top: 12, bottom: 60, left: 12, right: 12),
                child: CustomScrollView(
                  slivers: [
                    SliverToBoxAdapter(child: PaymentsAddressWidget(userAddresses: data.userAddresses)),
                    SliverToBoxAdapter(child: PaymentsGoodsWidget(goods: data.goods)),
                    const SliverToBoxAdapter(child: PaymentsInformationWidget()),
                    SliverToBoxAdapter(child: PaymentsPriceWidget(summary: data.summary))
                  ],
                ),
              ),
              PaymentsFooterWidget(totalPayPrice: data.summary.totalPayPrice),
            ],
          );
        },
        error: (error, StackTrace) {
          return const Text("數據錯誤");
        },
        loading: () {
          return const Text("數據加載中");
        },
      ),
    );
  }
}
